import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import {ReplaySubject} from 'rxjs';
import {DatasService} from '../data/data.service';
import {ChartPieService} from './chart-pie.service';
import {ChartLineService} from './chart-line.service';
import {ChartTableService} from './chart-table.service';
import {ChartKpiService} from './chart-kpi.service';
import {ChartScatterService} from './chart-scatter.service';
import {ChartRadarService} from './chart-radar.service';
import {ChartSankeyService} from './chart-sankey.service';
import {ChartContrastService} from './chart-contrast.service';
import {ChartWordcloudService} from './chart-wordcloud.service';
declare var $: any;
import * as echarts from 'echarts';
import Echarts = echarts.Echarts;
import * as _ from 'underscore';


@Injectable()
export class ChartService {

    constructor(
      private dataService: DatasService,
      private chartPieService: ChartPieService,
      private chartLineService: ChartLineService,
      private chartTableService: ChartTableService,
      private chartKpiService: ChartKpiService,
      private chartScatterService: ChartScatterService,
      private chartRadarService: ChartRadarService,
      private chartSankeyService: ChartSankeyService,
      private chartContrastService: ChartContrastService,
      private chartWorldCloudService: ChartWordcloudService

    ) {}

   public render(containerDom, widget, optionFilter, scope, reload, persist, relations){

     return new Promise(resolve => {
       // console.log(JSON.stringify(widget));
       // console.log(JSON.stringify(reload));
       var chart = this.getChartServices(widget.config);
       var that = this;
       this.dataService.getDataSeries(widget.datasource, widget.query, widget.datasetId, widget.config, function (data) {
         try {
           var option = chart.parseOption(data);
           if (optionFilter) {
             optionFilter(option); //！！！！！
           }
           if (data.drill) {
             data.drill.drillDown = function (id, value, render) {
               that.dataService.getDrillPath(widget.datasetId, id).then(function (path:any) {
                 var i = 0;
                 _.each(path, function (e:any, _i) {
                   if (e.id == id) {
                     i = _i;
                   }
                 });
                 var node = path[++i];
                 _.find(widget.config.keys, function (e:any, _i) {
                   if (e.id == id) {
                     e.type = '=';
                     e.values = [value];
                     if (!_.find(widget.config.keys, function (e:any) {
                         return e.id == node.id;
                       })) {
                       widget.config.keys.splice(_i + 1, 0, node);
                     }
                     return true;
                   }
                 });
                 _.find(widget.config.groups, function (e:any, _i) {
                   if (e.id == id) {
                     e.type = '=';
                     e.values = [value];
                     if (!_.find(widget.config.groups, function (e:any) {
                         return e.id == node.id;
                       })) {
                       widget.config.groups.splice(_i + 1, 0, node);
                     }
                     return true;
                   }
                 });
                 that.dataService.getDataSeries(widget.datasource, widget.query, widget.datasetId, widget.config, function (data) {
                   var option = chart.parseOption(data);
                   if (optionFilter) {
                     optionFilter(option);
                   }
                   render(option, data.drill.config);
                 },true);
               });
             };
             data.drill.drillUp = function (id, render) {
               _.find(widget.config.keys, function (e:any, _i) {
                 if (e.id == id) {
                   widget.config.keys[_i - 1].values = [];
                   widget.config.keys.splice(_i, 1);
                   return true;
                 }
               });
               _.find(widget.config.groups, function (e:any, _i) {
                 if (e.id == id) {
                   widget.config.groups[_i - 1].values = [];
                   widget.config.groups.splice(_i, 1);
                   return true;
                 }
               });
               this.dataService.getDataSeries(widget.datasource, widget.query, widget.datasetId, widget.config, function (data) {
                 var option = chart.parseOption(data);
                 if (optionFilter) {
                   optionFilter(option);
                 }
                 render(option, data.drill.config);
               });
             };
           }
         } finally {
           if (widget.config.chart_type == 'chinaMapBmap') {
             chart.render(containerDom, option, scope, persist, data.drill);
           } else {
             return resolve(chart.render(containerDom, option, scope, persist, data.drill, relations, widget.config));
           }
         }
       }, reload);
       return resolve();
     });

   }

   public realTimeRender(realTimeTicket, widget, optionFilter, scope, widgetWraper, isParamEvent){
     if (!realTimeTicket) {
       return;
     }
     var chart = this.getChartServices(widget.config);

     if (isParamEvent && widgetWraper) {
       widgetWraper.loading = true;
     }

     var callback = function (data) {
       var option = chart.parseOption(data);
       if (optionFilter) {
         optionFilter(option);
       }
       realTimeTicket(option, data.drill ? data.drill.config : null);
       if (widgetWraper) {
         widgetWraper.loading = false;
       }
     };
     this.dataService.getDataSeries(widget.datasource, widget.query, widget.datasetId, widget.config, callback, true);

   }
   public getChartServices(chartConfig){
     var chart;
     switch (chartConfig.chart_type) {
       case 'line':
         chart = this.chartLineService;
         break;
       case 'pie':
         chart = this.chartPieService;
         break;
       case 'kpi':
         chart = this.chartKpiService;
         break;
       case 'table':
         chart = this.chartTableService;
         break;
       case 'funnel':
         // chart = chartFunnelService;
         break;
       case 'sankey':
         chart = this.chartSankeyService;
         break;
       case 'radar':
         chart = this.chartRadarService;
         break;
       case 'map':
         // chart = chartMapService;
         break;
       case 'scatter':
         chart = this.chartScatterService;
         break;
       case 'gauge':
         // chart = chartGaugeService;
         break;
       case 'wordCloud':
         chart = this.chartWorldCloudService;
         break;
       case 'treeMap':
         // chart = chartTreeMapService;
         break;
       case 'areaMap':
         // chart = chartAreaMapService;
         break;
       case 'heatMapCalendar':
         // chart = chartHeatMapCalendarService;
         break;
       case 'heatMapTable':
         // chart = chartHeatMapTableService;
         break;
       case 'liquidFill':
         // chart = chartLiquidFillService;
         break;
       case 'contrast':
         chart = this.chartContrastService;
         break;
       case 'chinaMap':
         // chart = chartChinaMapService;
         break;
       case 'chinaMapBmap':
         // chart = chartChinaMapBmapService;
         break;
       case 'relation':
         // chart = chartRelationService;
         break;
     }
     return chart;
   }
}
